﻿package tools.utils {

	/**
	 * 处理日期的静态类
	 * @author ase7en
	 * @date 2010-7-9
	 */
	public class DateFormatter {
		/**
		 * 返回格式化后的日期
		 * @param theDate 当期系统时间
		 * @param mask 返回格式
		 * @example 按照月-日-年的格式显示时间
		 * <LISTING>
		 * trace(DateFormatter.formatTo(new Date(), "mm-dd-yyyy"));
		 * </LISTING>
		*/
		public static function formatTo(theDate : Date, mask : String) : String {
		
			var months : Array = new Array();
			var days : Array = new Array();
		
			months["Jan"] = {m:1, n:"January"};
			months["Feb"] = {m:2, n:"February"};
			months["Mar"] = {m:3, n:"March"};
			months["Apr"] = {m:4, n:"April"};
			months["May"] = {m:5, n:"May"};
			months["Jun"] = {m:6, n:"June"};
			months["Jul"] = {m:7, n:"July"};
			months["Aug"] = {m:8, n:"August"};
			months["Sep"] = {m:9, n:"September"};
			months["Oct"] = {m:10, n:"October"};
			months["Nov"] = {m:11, n:"November"};
			months["Dec"] = {m:12, n:"December"};
		
			days["Sun"] = {m:1, n:"Sunday"};
			days["Mon"] = {m:1, n:"Monday"};
			days["Tue"] = {m:1, n:"Tuesday"};
			days["Wed"] = {m:1, n:"Wednesday"};
			days["Thu"] = {m:1, n:"Thursday"};
			days["Fri"] = {m:1, n:"Friday"};
			days["Sat"] = {m:1, n:"Saturday"};
		
			// verify Date object being sent
			if (!(theDate is Date)) 
			{
				//	trace("Error in dateFormat - first parameter must be a date object");
				return "Error -- Date Object missing";
			}
		
			var parts:Array = theDate.toString().split(" ");
			var dayOfWeek:String = parts[0];
			var month:String = parts[1];
			var day:String = parts[2];
			var year:String = parts[5];
		
			var retString:String = "";
			
			// search for the mask words and replace with the appropriate mask
			if (mask.indexOf("short") != -1) 
			{
				mask = mask.split("short").join("m/d/y");
			} else if (mask.indexOf("medium") != -1) 
			{
				mask = mask.split("medium").join("mmm d, yyyy");
			} else if (mask.indexOf("long") != -1) 
			{
				mask = mask.split("long").join("mmmm d, yyyy");
			} else if (mask.indexOf("full") != -1) 
			{
				mask = mask.split("full").join("dddd, mmmm d, yyyy");
			}
		
			// used to store what character set we're processing
			var currentlyProcessing:String = '';
			// how many of the characters have we run into?
			var count:Number = 0;
	
			// loop through the format and replace values when we can
			for (var i:int = 0;i < mask.length;i++) {
				switch (mask.charAt(i)) {
					case 'd':
					case 'm':
					case 'y':
						// if we've been processing one set of characters and
						// stumbled across a new set, we need to update
						// the return string
						if (currentlyProcessing != '') 
						{
							if (currentlyProcessing != mask.charAt(i)) 
							{
								// processing a new character, so figure out what we've 
								// been processing, update the processing character,
								// and re-set the count
								retString += maskReplace(currentlyProcessing, count, dayOfWeek, month, day, year, months, days);
								currentlyProcessing = mask.charAt(i);
								count = 0;
							} 
						} 
						else 
						{
							currentlyProcessing = mask.charAt(i);	
						}
						count++;
						break;
				
					default:
						// we're stumbled upong a character thats not a mask character
						// so if we've been processing something, replace it
						// for the return string and reset what we've processing
						// to nothing
						if (currentlyProcessing != '') 
						{
							retString += maskReplace(currentlyProcessing, count, dayOfWeek, month, day, year, months, days);
							count = 0;
							currentlyProcessing = '';
						}
						// just add the character we've run into to the return string
						retString += mask.charAt(i);
				}
			}
	
			// after we're done, we need to check to see if we're still processing anything
			if (count > 0) 
			{
				retString += maskReplace(currentlyProcessing, count, dayOfWeek, month, day, year, months, days);
			}
	
			// finally, return the string with the mask replace with actual values
			return retString;
		}

		/*
		Darron Schall
		August 27, 2003
	
		Private "helper" function for dateFormat. Not meant to be called directly.
		 */
		private static function maskReplace(which:String, count:Number, dayOfWeek:String, month:String, day:String, year:String, months:Array, days:Array):* {
			switch(which) {
				case 'd':
					switch (count) {
						case 1: 
							return day;
						case 2: 
							return (Number(day) < 10) ? "0" + day : day;
						case 3: 
							return dayOfWeek;
						case 4: 
							return days[dayOfWeek].n;
					}
					break;
				case 'y':
					switch (count) {
						case 1: 
							return parseInt(year.substr(2, 2));
						case 2: 
							return year.substr(2, 2);
						case 4: 
							return year;
					}
			
				case 'm':
					switch (count) {
						case 1: 
							return months[month].m;
						case 2: 
							return (months[month].m < 10) ? "0" + months[month].m : months[month].m;
						case 3: 
							return month;
						case 4: 
							return months[month].n;
					}
			}
		}
	}
}
